package com.cow.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cow.dao.ShoppingCartMapper;
import com.cow.entity.ShoppingCart;
import com.cow.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper, ShoppingCart> implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public List<Map<String, Object>> queryCartWithAccount(String accountNumber) {
        return shoppingCartMapper.selectCartWithAccount(accountNumber);
    }

    @Override
    public boolean addShoppingCart(ShoppingCart shoppingCart) {
        //判断商品id、规格，账户是否和已存在的购物车信息相同，只修改数量，否则才添加
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper();
        wrapper.eq("product_id", shoppingCart.getProductId())
                .eq("product_specs", shoppingCart.getProductSpecs())
                .eq("account_number", shoppingCart.getAccountNumber());
        ShoppingCart cart = this.getOne(wrapper);
        boolean b = true;
        if (cart != null) {
            cart.setPayAmount(cart.getPayAmount() + shoppingCart.getPayAmount());
            b = this.updateById(cart);
        } else {
            b = this.save(shoppingCart);
        }
        return b;
    }
}
